package com.jxdinfo.hussar.cloud.module.example.lift.service.impl;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.cloud.module.example.lift.dao.LiftDataHistoryDao;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.DeviceInfoDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftDataHistoryDto;
import com.jxdinfo.hussar.cloud.module.example.lift.dto.LiftDataHistorySearchDto;
import com.jxdinfo.hussar.cloud.module.example.lift.model.Lift;
import com.jxdinfo.hussar.cloud.module.example.lift.model.LiftDataHistory;
import com.jxdinfo.hussar.cloud.module.example.lift.model.LiftHistoryCount;
import com.jxdinfo.hussar.cloud.module.example.lift.service.LiftDataHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 升降机历史数据(LiftDataHistory)表服务实现类
 *
 * @author makejava
 * @since 2023-10-23 18:18:25
 */
@Service("liftDataHistoryService")
public class LiftDataHistoryServiceImpl extends ServiceImpl<LiftDataHistoryDao, LiftDataHistory> implements LiftDataHistoryService {
    @Resource
    private LiftDataHistoryDao liftDataHistoryDao;
    @Override
    public List<LiftDataHistory> findLineChart(LiftDataHistorySearchDto dto) {
        //封装查询条件
        LambdaQueryWrapper<LiftDataHistory> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),LiftDataHistory::getTyProjectCode,dto.getTyProjectCode())
                .eq(ObjectUtils.isNotEmpty(dto.getLiftId()),LiftDataHistory::getLiftId,dto.getLiftId())
                .eq(ObjectUtils.isNotEmpty(dto.getDataType()),LiftDataHistory::getDataType,dto.getDataType())
                .ge(ObjectUtils.isNotEmpty(dto.getCollectionTimeStart()),LiftDataHistory::getCollectionTime,dto.getCollectionTimeStart())
                .le(ObjectUtils.isNotEmpty(dto.getCollectionTimeEnd()),LiftDataHistory::getCollectionTime,dto.getCollectionTimeEnd()+" 23:59:59")
                .orderByDesc(LiftDataHistory::getCollectionTime);
        List<LiftDataHistory> liftDataHistoryList = liftDataHistoryDao.selectList(queryWrapper);

        return liftDataHistoryList;
    }

    /**
     * 历史数据
     * @param dto
     * @return
     */
    @Override
    public Page<LiftDataHistory> findPage(LiftDataHistorySearchDto dto) {
        //封装查询条件
        LambdaQueryWrapper<LiftDataHistory> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ObjectUtils.isNotEmpty(dto.getTyProjectCode()),LiftDataHistory::getTyProjectCode,dto.getTyProjectCode())
                .eq(ObjectUtils.isNotEmpty(dto.getLiftId()),LiftDataHistory::getLiftId,dto.getLiftId())
                .eq(ObjectUtils.isNotEmpty(dto.getDataType()),LiftDataHistory::getDataType,dto.getDataType())
                .ge(ObjectUtils.isNotEmpty(dto.getCollectionTimeStart()),LiftDataHistory::getCollectionTime,dto.getCollectionTimeStart())
                .le(ObjectUtils.isNotEmpty(dto.getCollectionTimeEnd()),LiftDataHistory::getCollectionTime,dto.getCollectionTimeEnd()+" 23:59:59")
                .orderByDesc(LiftDataHistory::getCollectionTime);
        Page<LiftDataHistory> page = new Page<>(dto.getPage(),dto.getRows());
        Page<LiftDataHistory> historyPage = liftDataHistoryDao.selectPage(page, queryWrapper);

        if(historyPage.getRecords().size() > 0) {

//            //获取升降机设备状态
//            Map<String, DeviceInfoDto> deviceInfoDtoMap = deviceService.findDeviceAndLocationByProductType(DeviceTypeEnum.DEVICE_TYPE_TOWER, liftDataHistorySearchDto.getTyProjectCode());
//
//            List<Lift> lifts = liftDao.findListByProperty("tyProjectCode", liftDataHistorySearchDto.getTyProjectCode());
//            Map<String, String> map = lifts.stream().collect(Collectors.toMap(Lift::getId, Lift::getLiftName));
//            for(LiftDataHistory liftDataHistory : pageResult.getRows()) {
//                liftDataHistory.setLiftName(map.get(liftDataHistory.getLiftId()));
//
//                //根据升降机ID获取升降机信息
//                Lift lift = liftDao.findByProperty("id", liftDataHistory.getLiftId());
//                DeviceInfoDto liftDeviceInfoDto = deviceInfoDtoMap.get(lift.getDeviceCode());
//                //获取设备状态重新赋值
//                if (liftDeviceInfoDto != null) {
//                    if(liftDeviceInfoDto.getOnlineStatus()==0){
//                        liftDataHistory.setDeviceStatus(2);
//                    }else if(liftDeviceInfoDto.getOnlineStatus()==1){
//                        liftDataHistory.setDeviceStatus(0);
//                    }
//                }else{
//                    liftDataHistory.setDeviceStatus(2);
//                }
//            }
        }

        return historyPage;
    }
}

